package com.atguigu.transform;

import com.atguigu.been.WaterSensor;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;

/**
 * maxby 将这个字段的 所有的值都改变而非只有比较的字段
 *
 * @author wky
 * @create 2021-07-14-11:30
 */
public class Transform_MaxBy {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment senv = StreamExecutionEnvironment.getExecutionEnvironment();
        //senv.setParallelism(1);
        ArrayList<WaterSensor> waterSensors = new ArrayList<>();
        waterSensors.add(new WaterSensor("sensor_1", 1607527992000L, 20));
        waterSensors.add(new WaterSensor("sensor_1", 1607527994000L, 50));
        waterSensors.add(new WaterSensor("sensor_1", 1607527996000L, 50));
        waterSensors.add(new WaterSensor("sensor_2", 1607527993000L, 10));
        waterSensors.add(new WaterSensor("sensor_2", 1607527995000L, 30));
        DataStreamSource<WaterSensor> streamSource = senv.fromCollection(waterSensors);
        KeyedStream<WaterSensor, String> keyedStream = streamSource.keyBy(new KeySelector<WaterSensor, String>() {
            @Override
            public String getKey(WaterSensor waterSensor) throws Exception {
                return waterSensor.getId();
            }
        });
//        keyedStream.maxBy("vc",false).print(); //false 的当有相同最大值时 其他字段取新值
        keyedStream.maxBy("vc",true).print();// true 的当有相同最大值时 其他字段值不改变
//        keyedStream.minBy("vc",false); //同理
        senv.execute();
    }

}
